# 冒泡排序
def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]  # 交换
    return arr

# 选择排序
def selection_sort(arr):
    n = len(arr)
    for i in range(n):
        min_idx = i
        for j in range(i+1, n):
            if arr[j] < arr[min_idx]:
                min_idx = j
        arr[i], arr[min_idx] = arr[min_idx], arr[i]  # 交换
    return arr

# 归并排序
def merge_sort(arr):
    if len(arr) > 1:
        mid = len(arr) // 2  # 找到中间
        left_half = arr[:mid]  # 切分左半
        right_half = arr[mid:]  # 切分右半

        merge_sort(left_half)  # 递归排序左半
        merge_sort(right_half)  # 递归排序右半

        i = j = k = 0
        # 合并过程
        while i < len(left_half) and j < len(right_half):
            if left_half[i] < right_half[j]:
                arr[k] = left_half[i]
                i += 1
            else:
                arr[k] = right_half[j]
                j += 1
            k += 1

        # 处理剩余元素
        while i < len(left_half):
            arr[k] = left_half[i]
            i += 1
            k += 1
        while j < len(right_half):
            arr[k] = right_half[j]
            j += 1
            k += 1

    return arr

# 测试数据
data = [9, 3, 6, 5, 2, 7, 8, 1]

# 冒泡排序
sorted_bubble = bubble_sort(data.copy())
print("冒泡排序结果:", sorted_bubble)

# 选择排序
sorted_selection = selection_sort(data.copy())
print("选择排序结果:", sorted_selection)

# 归并排序
sorted_merge = merge_sort(data.copy())
print("归并排序结果:", sorted_merge)